﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblMediaRoomDAL:Iapp_tblMediaRoom
    {
        HKEA_CMSEntities model;
        app_tblMediaRoom mediaRoom;
        private app_tblMediaRoomDAL()
        {
            model = DataAccess.GetModelInstance();
            mediaRoom = model.app_tblMediaRoom.CreateObject();
        }

        public static app_tblMediaRoomDAL GetInstance()
        {
            return new app_tblMediaRoomDAL();
        }

        public app_tblMediaRoom GetEntity(int uid) {

            return model.app_tblMediaRoom.Where(a => a.MediaRoomUID == uid).SingleOrDefault();
        }
        public app_tblMediaRoom GetEntity(app_tblMediaRoom ent)
        {
            return model.app_tblMediaRoom.Where(a => a.AppMediaRoomUID == ent.AppMediaRoomUID && a.CustomerUID==ent.CustomerUID && a.IsDeleted==0).SingleOrDefault();
        }
        public app_tblMediaRoom GetLastEntity() {
            return model.app_tblMediaRoom.Where(a => a.AppMediaRoomUID > 0).OrderByDescending(a => a.CreatedAt).FirstOrDefault();
        }

        public int InsertENT(app_tblMediaRoom ent)
        {
            ent.CreatedAt = DateTime.Now;
            ent.UpdatedAt = DateTime.Now;
            model.app_tblMediaRoom.AddObject(ent);
            int i = model.SaveChanges();
            if(i>0)
                return model.app_tblMediaRoom.Max(a=>a.AppMediaRoomUID);
            return 0;
        }
        public List<app_tblMediaRoom> GetListByENT(app_tblMediaRoom ent)
        {
            var list = model.app_tblMediaRoom.Where(a=>a.AppMediaRoomUID > 0);
            if (!string.IsNullOrEmpty(ent.MediaType))
                list = list.Where(a => a.MediaType == ent.MediaType);
            if (!string.IsNullOrEmpty(ent.Category))
                list = list.Where(a => a.Category == ent.Category);
            if (ent.CustomerUID > 0)
                list = list.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.FileAlt_Eng))
                list = list.Where(a => a.FileAlt_Eng == ent.FileAlt_Eng);
            if (!string.IsNullOrEmpty(ent.FileAlt_Chi))
                list = list.Where(a => a.FileAlt_Chi == ent.FileAlt_Chi);
            if (!string.IsNullOrEmpty(ent.Des_Eng))
                list = list.Where(a => a.Des_Eng == ent.Des_Eng);
            if (ent.IsPublish >= 0)
                list = list.Where(a => a.IsPublish == ent.IsPublish);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                list = list.Where(a => a.LastModifyBy == ent.LastModifyBy);            
            return list.ToList();
        }

        public List<MediaRoomList> GetList(app_tblMediaRoom ent, bool IsAddDeletedForSelect)
        {
            var ret = from room in model.app_tblMediaRoom
                      select new MediaRoomList()
                      {
                          AppKeyUID = room.AppMediaRoomUID,
                          CateGory = room.Category,
                          CustomerUID = (int)room.CustomerUID,
                          FileName = room.FileName,
                          FilePath = room.FilePath,
                          From = "app",
                          KeyUID = room.AppMediaRoomUID,
                          MediaType = room.MediaType,
                          TblKeyUID = room.MediaRoomUID > 0 ? (int)room.MediaRoomUID : 0,
                          IsDeleted=room.IsDeleted
                      };
            if (ent.AppMediaRoomUID > 0)
                ret = ret.Where(a=>a.AppKeyUID==ent.AppMediaRoomUID);
            if (ent.CustomerUID > 0)
                ret = ret.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a=>a.TblKeyUID==ent.MediaRoomUID);
            if (IsAddDeletedForSelect)
                ret = ret.Where(a=>a.IsDeleted==ent.IsDeleted);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a=>a.CateGory==ent.Category);
            if (!string.IsNullOrEmpty(ent.MediaType))
                ret = ret.Where(a=>a.MediaType==ent.MediaType);
            return ret.OrderByDescending(a => a.AppKeyUID).ToList();
        }

        public int GetCountByENT(app_tblMediaRoom ent) {
            var result = model.app_tblMediaRoom.Where(a => a.MediaRoomUID > 0);
            if (!string.IsNullOrEmpty(ent.MediaType))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.FileAlt_Eng))
                result = result.Where(a => a.FileAlt_Eng == ent.FileAlt_Eng);
            if (!string.IsNullOrEmpty(ent.FileAlt_Chi))
                result = result.Where(a => a.FileAlt_Chi == ent.FileAlt_Chi);
            if (!string.IsNullOrEmpty(ent.Des_Eng))
                result = result.Where(a => a.Des_Eng == ent.Des_Eng);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(app_tblMediaRoom ent)
        {
            var Temp = model.app_tblMediaRoom.Where(a=>a.AppMediaRoomUID==ent.AppMediaRoomUID).FirstOrDefault();
            if (Temp != null)
            {
                Temp.AliasName = ent.AliasName;
                Temp.Category = ent.Category;
                Temp.CustomerUID = ent.CustomerUID;
                Temp.Des_Chi = ent.Des_Chi;
                Temp.Des_Eng = ent.Des_Eng;
                Temp.FileAlt_Chi = ent.FileAlt_Chi;
                Temp.FileAlt_Eng = ent.FileAlt_Eng;
                Temp.FileExt = ent.FileExt;
                Temp.FileLink_Chi = ent.FileLink_Chi;
                Temp.FileLink_Eng = ent.FileLink_Eng;
                Temp.FileName = ent.FileName;
                Temp.FilePath = ent.FilePath;
                Temp.IsPublish = ent.IsPublish;
                Temp.MediaRoomUID = ent.MediaRoomUID;
                Temp.MediaType = ent.MediaType;
                Temp.UpdatedAt = DateTime.Now;
                return model.SaveChanges();
            }
            return 0;
        }
        public int DeleteENT(app_tblMediaRoom ent)
        {
            var Temp = model.app_tblMediaRoom.Where(a => a.AppMediaRoomUID == ent.AppMediaRoomUID).FirstOrDefault();
            if (Temp != null)
            {
                model.app_tblMediaRoom.DeleteObject(Temp);
            }
            return model.SaveChanges(0);
        }
    }
}
